import { getSlider } from '@/api/public';
import WS from '@/utils/websocket';
import * as types from './action-types.js';//存放所有方法的类型，为了直观看到哪些方法调用

console.log(types)

const rootModule = {
  state: {
    sliders: [],
    ws: null,
    message: ''
  },
  mutations: {
    [types.SET_SLIDERS](state, payload) {
      console.log(state, payload, 'mutations=>state,payload');
      state.sliders = payload;
    },
    [types.CREATE_WEBSOCKET](state, ws) {
      console.log(ws,'ws\n');
      //存放ws信息
      state.ws = ws
    },
    [types.SET_MESSAGE](state, msg) {   //存放msg信息
      console.log(msg,'msg\n');
      state.message = msg;
    }
  },
  actions: {
    //调用getSlider()的api
    async [types.SET_SLIDERS]({ commit }, payload) {
      console.log(commit, payload, 'actions=>commit,payload');
      const { data } = await getSlider();
      console.log(data, '数据')
      commit(types.SET_SLIDERS, data)
    },
    async [types.CREATE_WEBSOCKET]({ commit }) {
      let ws = new WS();
      ws.create(); //调用src/utils/websocket.js中create的方法
      commit(types.CREATE_WEBSOCKET, ws);
    }
  },
  modules: {}
}

export default rootModule;